package product

import (
	"context"
	"github.com/gogf/gf/v2/util/gconv"
	"home-butler/api/product"
	v1 "home-butler/api/product/v1"
	"home-butler/internal/model"
	"home-butler/internal/service"
)

type ControllerProductTopicV1 struct{}

func NewProductTopicV1() product.IProductTopicV1 {
	return &ControllerProductTopicV1{}
}

func (c ControllerProductTopicV1) ListProductTopic(ctx context.Context, req *v1.ProductTopicListReq) (res []*v1.ProductTopicPageRes, err error) {
	data := model.ProductTopicListReq{}
	if err = gconv.Struct(req, &data); err != nil {
		panic(err)
	}
	out, err := service.ProductTopic().ListProductTopic(ctx, &data)
	res = make([]*v1.ProductTopicPageRes, 0)
	err = gconv.Structs(out, &res)
	if err != nil {
		panic(err)
	}
	return
}

func (c ControllerProductTopicV1) CreateProductTopic(ctx context.Context, req *v1.ProductTopicReq) (res *v1.ProductTopicRes, err error) {
	data := model.ProductTopicReq{}
	if err = gconv.Struct(req, &data); err != nil {
		panic(err)
	}
	out, err := service.ProductTopic().CreateProductTopic(ctx, &data)
	err = gconv.Structs(out, &res)
	if err != nil {
		panic(err)
	}
	return
}

func (c ControllerProductTopicV1) UpdateProductTopic(ctx context.Context, req *v1.ProductTopicUpdateReq) (res *v1.ProductTopicRes, err error) {
	data := model.ProductTopicUpdateReq{}
	if err = gconv.Struct(req, &data); err != nil {
		panic(err)
	}
	out, err := service.ProductTopic().UpdateProductTopic(ctx, &data)
	err = gconv.Structs(out, &res)
	if err != nil {
		panic(err)
	}
	return
}

func (c ControllerProductTopicV1) DelProductTopic(ctx context.Context, req *v1.ProductTopicDelReq) (res *v1.ProductTopicRes, err error) {
	data := model.ProductTopicDelReq{}
	if err = gconv.Struct(req, &data); err != nil {
		panic(err)
	}
	out, err := service.ProductTopic().DelProductTopic(ctx, &data)
	err = gconv.Structs(out, &res)
	if err != nil {
		panic(err)
	}
	return
}
